package com.winsdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winsdom.dto.RoleDTO;
import com.winsdom.dto.RoleManageDto;
import com.winsdom.entity.SysPermission;
import com.winsdom.entity.SysRole;
import com.winsdom.entity.SysRolePermission;
import com.winsdom.mapper.SysPermissionMapper;
import com.winsdom.mapper.SysRoleMapper;
import com.winsdom.mapper.SysRolePermissionMapper;
import com.winsdom.result.PageResult;
import com.winsdom.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winsdom.vo.RoleInformationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-12
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    /**
     * 获取角色名称
     * @return
     */
    @Override
    public List<RoleInformationVO> getAllRole() {
        List<RoleInformationVO> roleInformationVO=sysRoleMapper.getAllRole();
        return roleInformationVO;
    }

    /**
     * 分页条件查询角色信息
     * @param roleDTO
     * 前端传入信息
     * @return
     * 返回pageResult
     */
    @Override
    public PageResult findRolesBatch(RoleDTO roleDTO) {

        //查询条件包装器
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();

        //判断查询条件是否为空
        if (StringUtils.hasText(roleDTO.getRoleName())) {
            queryWrapper.like("role_name",roleDTO.getRoleName());
        }
        if (roleDTO.getStatus() != null){
            queryWrapper.eq("status",roleDTO.getStatus());
        }

        //按照条件分页查询
        Page<SysRole> page = sysRoleMapper.selectPage(new Page<>(roleDTO.getPageNum(), roleDTO.getPageSize()), queryWrapper);

        //创建一个PageResult对象并赋值
        PageResult pageResult=new PageResult();
        pageResult.setRecords(page.getRecords());
        pageResult.setTotal(page.getTotal());
        pageResult.setPageNum(roleDTO.getPageNum());
        pageResult.setPageSize(roleDTO.getPageSize());

        return pageResult;

    }

    @Override
    @Transactional
    public int addRole(RoleManageDto addRoleDTO) {
        int i=sysRoleMapper.selectRoles(addRoleDTO.getRoleName());
        if(i!=0){
            return 0;
        }
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(addRoleDTO.getRoleName());
        sysRole.setStatus(1);
        sysRole.setIsDeleted(0);
        sysRoleMapper.insert(sysRole);
        Long id = sysRole.getId();
        SysRolePermission sysRolePermission = new SysRolePermission();
        sysRolePermission.setRoleId(id);
        List<SysPermission> list=sysPermissionMapper.selectList(null);

        for (Long pid : addRoleDTO.getPermissionIds()) {
            for(SysPermission sysPermission: list){
                if(sysPermission.getId()==pid){
                    sysRolePermission.setPermissionId(pid);
                    sysRolePermissionMapper.insert(sysRolePermission);
                    break;
                }
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int disableRole(Long roleId) {
        //验证是否有账号关联角色
        if (sysRoleMapper.selectRole(roleId)!= 0) {
            return 0;
        }else{
            return sysRoleMapper.disableRole(roleId);
        }
    }

    @Override
    public Long enableRole(Long roleId) {
        return sysRoleMapper.enableRole(roleId);
    }
}
